﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DoeObjects;
using System.Data;
using System.Data.SqlClient;

namespace DoeLibsTest
{
    public class Helper
    {
		private static Random numberRandomizer = new Random();
        /// <summary>
        /// generates a random string
        /// </summary>
        /// <returns></returns>
        public static string getRandomString()
        {
            Guid g = Guid.NewGuid();
            return Convert.ToBase64String(g.ToByteArray());
        }

		public static string getEnoughRandomEmail()
		{
			return "test" + numberRandomizer.Next().ToString() + "@gmail.come";
		}

        public static User createUserObject(bool staffCategory = false, bool savedInDatabase = false)
        {
            User u = new User();
            u.FirstName = getRandomString();
            u.LastName = getRandomString();
            u.Password = getRandomString();
            u.Salt = getRandomString();
            u.UniPhone = getRandomString();
            u.Room = getRandomString();
            u.Acceptor = null;
            u.Category = (staffCategory) ? UserCategory.getById(1) : UserCategory.getById(2);
            //u.EMail = getRandomString();
			u.EMail = getEnoughRandomEmail();

            if (savedInDatabase && !u.save())
            {
                throw new Exception("could not save user");
            }

            return u;
        }

        public static Author createAuthorObject(bool savedInDatabase = false) 
        {
            Author a = new Author();
            a.Name = getRandomString();

            if (savedInDatabase)
            {
                if (!a.save())
                {
                    throw new Exception("could not save author");
                }
            }

            return a;
        }

		public static Topic createTopicObject(bool savedInDatabase = false)
		{
			Topic topic = new Topic();
			topic.TopicName = getRandomString();

			if (savedInDatabase)
				if (!topic.save())
					throw new Exception("Could not save topic.");

			return topic;
		}

        public static Publisher createPublisher(bool savedInDatabase = false)
        {
            Publisher pub = new Publisher();
            pub.Name = getRandomString();

            if (savedInDatabase && !pub.save())
            {
                throw new Exception("could not save publisher");
            }

            return pub;
        }

        public static Title createTitle(bool savedInDatabase = false)
        {
            Random rand = new Random();
            Title t = new Title();
            t.BookTitle = getRandomString();
            t.EditionNumber = rand.Next();
            t.EditionYear = rand.Next();
            t.FirstEditionYear = rand.Next();
            t.ISBN10 = getRandomString().Substring(0, 10);
            t.ISBN13 = getRandomString().Substring(0, 13);
            t.Publisher = createPublisher(savedInDatabase);

            if (savedInDatabase && !t.save())
            {
                throw new Exception("could not save title");
            }

            return t;
        }

        private static LoanableCategory createLoanableCategory(bool saveInDatabase = false, User owner = null)
        {
            LoanableCategory cat = new LoanableCategory();
            cat.Name = getRandomString();
            cat.Owner = (owner == null) ? createUserObject(true, saveInDatabase) : owner;

            if (saveInDatabase && !cat.save())
            {
                throw new Exception("could not save loanablecategory");
            }

            return cat;
        }

        public static Loanable createLoanable(bool saveInDatabase = false, Title t = null)
        {
            Loanable l = new Loanable();
            l.Barcode = getRandomString().Substring(0, 5);
            l.Location = getRandomString().Substring(0, 4);
            l.Title = (t == null)?createTitle(saveInDatabase):t;
            l.Owner = createUserObject(true, saveInDatabase);
            l.Category = createLoanableCategory(saveInDatabase, l.Owner);

            if (saveInDatabase && !l.save())
            {
                throw new Exception("could not save loanable");
            }

            return l;
        }

        public static Notification createNotification(NotificationType type, bool saveInDatabase = false, User sender = null, User recipient = null)
        {
            Notification notification = new Notification();
            notification.Done = false;
            notification.Read = false;
            notification.Recipient = (recipient == null)?createUserObject(false, saveInDatabase):recipient;
            notification.SendDate = DateTime.Now;
            notification.Sender = sender;
            notification.Type = type;
            notification.Message = getRandomString();

            if (saveInDatabase && !notification.save())
            {
                throw new Exception("could not save notification");
            }

            return notification;
        }

        /// <summary>
        /// deletes all entries from the reservations table
        /// </summary>
        /// <returns></returns>
        public static bool clearReservationsTable()
        {
            SqlConnection connection = new SqlConnection(Settings.ConnectionString);
            SqlCommand command = new SqlCommand("TRUNCATE TABLE Reservation", connection);
            bool success = true; 

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                //success = false;
                throw ex;
            }
            finally
            {
                if (connection != null && connection.State != ConnectionState.Closed)
                {
                    connection.Close();
                }
            }

            return success;
        }

        public static bool clearLoanTable()
        {
            SqlConnection connection = new SqlConnection(Settings.ConnectionString);
            SqlCommand command = new SqlCommand("TRUNCATE TABLE Loan", connection);
            bool success = true;

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                //success = false;
                throw ex;
            }
            finally
            {
                if (connection != null && connection.State != ConnectionState.Closed)
                {
                    connection.Close();
                }
            }

            return success;
        }

        public static bool clearNotificationTable()
        {
            SqlConnection connection = new SqlConnection(Settings.ConnectionString);
            SqlCommand command = new SqlCommand("TRUNCATE TABLE Notification", connection);
            bool success = true;

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                //success = false;
                throw ex;
            }
            finally
            {
                if (connection != null && connection.State != ConnectionState.Closed)
                {
                    connection.Close();
                }
            }

            return success;
        }
    }
}
